package org.example.util;

import org.example.ITree;
import org.example.RedBlackTree;

public class BTreeUtil {
    public static void inorderTraversal(ITree.ITreeNode<?, ?> root) {
        if (root == null) { return; }
        inorderTraversal(root.getLeft());
        System.out.println(root.getKey());
        inorderTraversal(root.getRight());
    }
    public static void printRBTree(RedBlackTree.RBNode<?, ?> root) {
        if (root == null) {
            System.out.println("this is a empty tree");
            return;
        }
        TreePrintUtil.pirnt(new RedBlackNodeAdapter(root));
    }

    public static void printTree(ITree.ITreeNode<?, ?> root) {
        if (root == null) {
            System.out.println("this is a empty tree");
            return;
        }
        TreePrintUtil.pirnt(new TreeNodeAdapter(root));
    }

    public static <K, V> ITree.ITreeNode<K, V> getBrother(ITree.ITreeNode<K, V> self){
        if (self == null) { return null; }
        ITree.ITreeNode<K, V> parent = self.getParent();
        if (parent == null) { return null; }
        if (parent.getRight() == self) {
            return parent.getLeft();
        } else {
            return parent.getRight();
        }
    }
}
